package com.smart.six.encryption;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * 扩展PropertyPlaceholderConfigurer支持解密属性值
 *
 * @author junrui.wang
 * @create 2018/7/11
 */
//集成PropertyPlaceholderConfigurer以支持加密解密
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private final String[] encryptPropertyNames = {"username", "password"};

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        //对特定属性值进行转换
        if(isEncryptProperty(propertyName)){
            String decryptPropertyValue = DESUtils.getDecryptString(propertyValue);
            System.out.println("decryptPropertyValue=" + decryptPropertyValue);
            return decryptPropertyValue;
        } else {
            return propertyValue;
        }
    }

    //判断是否是需要解密的属性值
    private boolean isEncryptProperty(String propertyName) {
        for (String property : encryptPropertyNames){
            if(propertyName.toLowerCase().indexOf(property) != -1){
                return true;
            }
        }
        return false;
    }
}
